package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;


import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.DeviceLoginEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.Device;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IDeviceStatusService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 设备离线推送至app
 */
@MsgType(MsgTypeEnum.DEVICE_OFFLINE)
@Component
public class OfflineHandlerImpl implements IMsgHandler {


    @Resource
    private IDeviceStatusService deviceStatusService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IAppCommService appCommService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        Device device = deviceService.getDeviceInfoByImei(messageBase.getDeviceKey());
        if (device != null) {
            deviceStatusService.downLine(device.getId());
            AppProtocol appProtocol = AppProtocol.builder()
                    .imei(device.getImei())
                    .cmdName(MsgTypeEnum.DEVICE_STATE_PUSH.getCmdName())
                    .data(DeviceLoginEnum.DEVICE_OFFLINE)
                    .build();
            appCommService.pushDeviceState(device.getId(), appProtocol);
        }
        return;
    }

}
